package com.galaxy.user.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.user.domain.UserFeedback;
import com.galaxy.user.domain.bo.UserFeedbackBo;
import com.galaxy.user.domain.vo.UserFeedbackVo;
import com.galaxy.user.mapper.UserFeedbackMapper;
import com.galaxy.user.service.UserFeedbackService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户反馈Service实现
 */
@RequiredArgsConstructor
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements UserFeedbackService {

    private final UserFeedbackMapper userFeedbackMapper;
    private final Converter converter;

    @DS("slave")
    @Override
    public TableDataInfo<UserFeedbackVo> queryPageList(UserFeedbackBo bo, PageQuery pageQuery) {
        UserFeedback userFeedback = converter.convert(bo, UserFeedback.class);
        Page<UserFeedback> userFeedbackPage = userFeedbackMapper.selectUserFeedbackList(userFeedback, pageQuery.build());
        List<UserFeedbackVo> convert = converter.convert(userFeedbackPage.getRecords(), UserFeedbackVo.class);
        return TableDataInfo.build(convert);
    }

    @DS("slave")
    @Override
    public List<UserFeedbackVo> queryList(UserFeedbackBo bo) {
        UserFeedback userFeedback = converter.convert(bo, UserFeedback.class);
        return userFeedbackMapper.selectUserFeedbackList(userFeedback).stream()
            .map(feedback -> converter.convert(feedback, UserFeedbackVo.class))
            .collect(Collectors.toList());
    }

    @DS("slave")
    @Override
    public UserFeedback queryById(Long id) {
        return userFeedbackMapper.selectUserFeedbackById(id);
    }

    @Override
    public Boolean insertByBo(UserFeedbackBo bo) {
        UserFeedback add = converter.convert(bo, UserFeedback.class);
        return userFeedbackMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(UserFeedbackBo bo) {
        UserFeedback update = converter.convert(bo, UserFeedback.class);
        return userFeedbackMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return userFeedbackMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean updateByDeviceId(UserFeedbackBo bo) {
        int update = userFeedbackMapper.update(new LambdaUpdateWrapper<UserFeedback>()
            .set(UserFeedback::getStatus, bo.getStatus())
            .set(UserFeedback::getReply, bo.getReply())
            .eq(UserFeedback::getDeviceId, bo.getDeviceId())
            .eq(UserFeedback::getFeedbackType, 2)
            .in(UserFeedback::getStatus, 0, 1));
        return update > 0;
    }
}
